﻿function addLoadEvent(func) { 
    var oldonload = window.onload; 
    if (typeof window.onload != 'function') window.onload = func;
    else { 
        window.onload = function() { 
            if (oldonload) oldonload();
            func(); 
        } 
    } 
}

function addUnLoadEvent(func) { 
    var oldonunload = window.onunload; 
    if (typeof window.onunload != 'function') window.onunload = func;
    else { 
        window.onunload = function() { 
            if (oldonunload) oldonunload();
            func(); 
        } 
    } 
}

function cslivemap(containerid, data)
{
    this.Containerid = containerid;
    this.map = null;
    this.Mappaths = data.mapdata;
}

cslivemap.prototype = {
    AddPushPin: function(Title, Description) {
        var shape = new VEShape(VEShapeType.Pushpin, this.map.GetCenter());
        shape.SetTitle(unescape(Title));
        shape.SetDescription(unescape(Description));
        this.map.AddShape(shape);
        this.map.SetZoomLevel(16);
    },
    Loaded: function() {        
        for (var x = 0; x < this.Mappaths.length; x++)
        {
            if (this.Mappaths[x].mode == "points")
            {
                for (var i = 0; i < this.Mappaths[x].points.length; i++)
                {
                    if (this.Mappaths[x].points[i].mode == "search") 
                    {
                        this.map.Find(null, this.Mappaths[x].points[i].searchfor);
                        setTimeout(this.Containerid + "_map.AddPushPin('" + escape(this.Mappaths[x].points[i].Title) + "', '" + escape(this.Mappaths[x].points[i].Description) + "')", 2000);
                    } 
                    else if (this.Mappaths[x].points[i].mode == "latlong")
                    {
                        var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(this.Mappaths[x].points[i].Lat, this.Mappaths[x].points[i].Long));
                        shape.SetTitle(this.Mappaths[x].points[i].Title);
                        shape.SetDescription(this.Mappaths[x].points[i].Description);
                        this.map.AddShape(shape);
                    }
                }
            }
            else if (this.Mappaths[x].mode == "paths")
            {
                for (var i = 0; i < this.Mappaths[x].paths.length; i++)
                {
                    var a = new Array();
                    for (var y = 0; y < this.Mappaths[x].paths[i].points.length; y++)
                        a.push(new VELatLong(this.Mappaths[x].paths[i].points[y].Lat, this.Mappaths[x].paths[i].points[y].Long));
                    if (this.Mappaths[x].paths[i].mode == "gon")
                    {
                        var shape = new VEShape(VEShapeType.Polygon, a);
                        shape.SetTitle(this.Mappaths[x].paths[i].Title);
                        shape.SetDescription(this.Mappaths[x].paths[i].Description);
                        var lineColor = new VEColor(parseInt(this.Mappaths[x].paths[i].Stroke.split(',')[1]),parseInt(this.Mappaths[x].paths[i].Stroke.split(',')[2]),parseInt(this.Mappaths[x].paths[i].Stroke.split(',')[3]),parseInt(this.Mappaths[x].paths[i].Stroke.split(',')[0]) / 255);
                        shape.SetLineColor(lineColor);
                        var fillColor = new VEColor(parseInt(this.Mappaths[x].paths[i].Fill.split(',')[1]),parseInt(this.Mappaths[x].paths[i].Fill.split(',')[2]),parseInt(this.Mappaths[x].paths[i].Fill.split(',')[3]),parseInt(this.Mappaths[x].paths[i].Fill.split(',')[0]) / 255);
                        shape.SetFillColor(fillColor);
                        this.map.AddShape(shape);
                    }
                    else
                    {
                        var shape = new VEShape(VEShapeType.Polyline, a);
                        shape.SetTitle(this.Mappaths[x].paths[i].Title);
                        shape.SetDescription(this.Mappaths[x].paths[i].Description);
                        var lineColor = new VEColor(parseInt(this.Mappaths[x].paths[i].Stroke.split(',')[1]),parseInt(this.Mappaths[x].paths[i].Stroke.split(',')[2]),parseInt(this.Mappaths[x].paths[i].Stroke.split(',')[3]),parseInt(this.Mappaths[x].paths[i].Stroke.split(',')[0]) / 255);
                        shape.SetLineColor(lineColor);
                        this.map.AddShape(shape);
                    }

                }
            }
            else if (this.Mappaths[x].mode == "collections")
            {
                for (var i = 0; i < this.mappaths[x].points.length; i++)
                {
                    if (this.Mappaths[x].collections[i].mode == "collection") 
                    {
                        var l = new VEShapeLayer();
                        var veLayerSpec = new VEShapeSourceSpecification(VEDataType.VECollection, this.Mappaths[x].collections[i].Collection, l);
                        this.map.ImportShapeLayerData(veLayerSpec, null, 1);
                    }
                    else if (this.Mappaths[x].collections[i].mode == "georss")
                    {
                        var l = new VEShapeLayer();
                        var veLayerSpec = new VEShapeSourceSpecification(VEDataType.GeoRSS, this.Mappaths[x].collections[i].GeoRss, l);
                        this.map.ImportShapeLayerData(veLayerSpec, null, 1);
                    }
                }
            }
        }
    }
}